package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsTopic;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsTopicMapper;
import com.itqf.service.DtsTopicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/17 16:22
 * description:
 */
@Service
public class DtsTopicServiceImpl implements DtsTopicService {
    @Autowired
    private DtsTopicMapper dtsTopicMapper;
    public static final Logger logger = LoggerFactory.getLogger(DtsAdminServiceImpl.class);
    @Override
    public Map findByPage(DtsSearchDto serachDto) {
        logger.info("[专题管理]业务开始调用,分页参数为:{}",serachDto);
        //启动分页插件
        PageHelper.startPage(serachDto.getPage(),serachDto.getLimit());
        //查询数据库
        List<DtsTopic> dtsTopicList = dtsTopicMapper.selectTopicByPage(serachDto);
        //pageInfo封装
        PageInfo<DtsTopic> pageInfo = new PageInfo<>(dtsTopicList);
        //封装结果返回数据
        Map data = new HashMap();
        data.put("total",pageInfo.getTotal());
        data.put("items",pageInfo.getList());
        logger.info("[专题管理]业务调用完毕",data);
        return data;
    }



    @Override
    public boolean checkTopic(String topic) {
        long count = dtsTopicMapper.selectTopic(topic);
        logger.debug("[添加专题]当前专题存在数量为",count,topic);
        return count >=1;
    }



    @Override
    public DtsTopic saveTopic(DtsTopic dtsTopic) {
        dtsTopic.setAddTime(new Date());
        dtsTopic.setDeleted(false);
        logger.debug("[添加专题]获取回显数据为:{}",dtsTopic);
        int rows = dtsTopicMapper.insertTopic(dtsTopic);
        return dtsTopic;
    }




    @Override
    public int deleteById(Integer id) {
        int rows = dtsTopicMapper.deleteByPrimaryKey(id);
        return rows;
    }



    @Override
    public int updateTopic(DtsTopic dtsTopic){
        dtsTopic.setUpdateTime(new Date());
        int rows = dtsTopicMapper.updateById(dtsTopic);
        logger.debug("[添加专题]获取失败回显数据为:{}",dtsTopic);
        return rows;
    }


}
